package com.lysj.bhp.blacklist.job;

import com.lysj.bhp.blacklist.base.BlacklistContent;
import com.lysj.bhp.blacklist.domain.Blacklist;
import com.lysj.bhp.borrowing.domain.BorrowingInstallment;
import com.lysj.bhp.borrowing.domain.BorrowingRecord;
import com.lysj.bhp.borrowing.domain.CreditFile;
import com.lysj.bhp.borrowing.domain.ServiceCharge;
import com.lysj.bhp.util.random.FormatUtil;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.wallet.domain.BhpcWallet;
import com.lysj.bhp.wallet.domain.BitCoinWallet;
import com.lysj.bhp.wallet.domain.EthereumWallet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

@Component
public class BlackListJob extends BlacklistContent {

    private static Logger logger = LoggerFactory.getLogger(BlackListJob.class);


    @Scheduled(cron = "00 00 00 * * ?")
    public void blackListTask() throws Exception {
        setup();
        addblacklist();
        releaseBlacklist();
    }


    public void setup() {
        logger.info("BlackList  setupJob is Starting ...");
        List<Long> userIds = borrowingInstallmentRepository.findOverdurUser(new Date());
        List<Blacklist> blacklists = new LinkedList<>();
        for (Long userId : userIds) {
            Blacklist blacklist = blacklistRepository.findByUserIdAndIsExist(userId, 1);
            if (blacklist == null) {
                blacklists.add(blicklistInitiallze(userId));
            }
            logger.info("黑名单 userId is" + userId);
        }
        blacklistRepository.save(blacklists);
    }

    /**
     * @author zwj
     * @date 2018-08-22 10:14
     * @@Description: 定时器判断黑名单
     */
    public void addblacklist() {
        //手续费
        ServiceCharge serviceCharge = serviceChargeRepository.findOne(2L);
        BigDecimal serviceRate = serviceCharge.getServiceRate().divide(new BigDecimal(100));
        //黑名单计算步骤
        //固定值设置
        //本天的开始时间
        Date nowDayStartDate = FormatUtil.getDayStartDay(new Date());
        Date nowDayEndDate = FormatUtil.getDayEndDay(new Date());
        Date releaseDate = FormatUtil.addDay(nowDayStartDate, 90);
        //查询所有逾期记录
        List<BorrowingInstallment> borrowingInstallments = borrowingInstallmentRepository.findOverdueRecoed(nowDayStartDate);
        if (borrowingInstallments.size() > 0) {
            //大于等于四天的违约次数
            int defaultFourAndMordDayAmount = 0;
            //逾期三天的违约次数
            int defaultThreeDayAmount = 0;
            //违约金
            BigDecimal totaldefaultMoney = new BigDecimal(BigInteger.ZERO);
            //增加的违约金
            BigDecimal newDefaultMoney = new BigDecimal(BigInteger.ZERO);
            //用户ID 取数组中的第一个
            Long userId = borrowingInstallments.get(0).getUserId();
            //黑名单状态
            Blacklist blacklist = blacklistRepository.findByUserIdAndIsExist(userId, 1);
            //总计违约次数
            int overdueAmount = blacklist.getOverdueAmount();
            //该用户有几笔逾期
            int overdueStrokeCount = 0;
            //初始逾期记录Id
            Long borrowingRecordId = 0L;
            //list borrowingInstallment 容器
            List<BorrowingInstallment> chageBorrowingInstallments = new LinkedList<>();
            //list blacklist 容器
            List<Blacklist> blacklists = new LinkedList<>();
            //list borrowingRecord 容器
            List<BorrowingRecord> borrowingRecords = new LinkedList<>();
            //list creditFile 容器
            List<CreditFile> creditFiles = new LinkedList<>();
            //三类钱包 初始化该钱包当前逾期未还期数 逾期未还金额
            CreditFile btcCreditFile = creditFileInit(userId, 10);
            CreditFile ethCreditFile = creditFileInit(userId, 20);
            CreditFile bhpcCreditFile = creditFileInit(userId, 100);
            Integer overdueLevel = null;
            for (BorrowingInstallment overdueRecoed : borrowingInstallments) {
                //逾期等级
                overdueLevel = blacklist.getOverdueLevel();
                //更新黑名单信息：要是此用户Id 不等于上一个记录中的ID 或者本条记录为最后一条
                //预留判断最后一条
                if (!overdueRecoed.getUserId().equals(userId)) {
                    //添加黑名单记录
                    addBlacklist(blacklists, creditFiles, defaultFourAndMordDayAmount, defaultThreeDayAmount, overdueAmount, blacklist, overdueLevel, nowDayStartDate, releaseDate, btcCreditFile, ethCreditFile, bhpcCreditFile);
                    //大于等于四天的违约次数
                    defaultFourAndMordDayAmount = 0;
                    //逾期三天的违约次数
                    defaultThreeDayAmount = 0;
                    //用户ID 取数组中的第一个
                    userId = overdueRecoed.getUserId();
                    //查询该用户黑名单信息
                    blacklist = blacklistRepository.findByUserIdAndIsExist(userId, 1);
                    //此黑名单用户总计违约次数
                    overdueAmount = blacklist.getOverdueAmount();
                    //此用户今日违约笔数
                    overdueStrokeCount = 0;
                    //初始化信用档案
                    btcCreditFile = creditFileInit(userId, 10);
                    ethCreditFile = creditFileInit(userId, 20);
                    bhpcCreditFile = creditFileInit(userId, 100);
                }
                //如果与前一条的违约记录的Id不用，则增加1笔违约记录
                if(!borrowingRecordId.equals(overdueRecoed.getBorrowingRecordId())){
                    borrowingRecordId = overdueRecoed.getBorrowingRecordId();
                    overdueStrokeCount += 1;
                }
                System.out.println("次数"+overdueStrokeCount);
                //逾期天数计算
                System.out.println(overdueRecoed.getEndRepaymentDate());
                System.out.println(nowDayEndDate);
                long differDays = FormatUtil.getDifferDays(overdueRecoed.getEndRepaymentDate(), nowDayEndDate);
                System.out.println(differDays + "逾期天数");
                //如果该笔没有被记录黑名单则将该笔加入黑名单
                if(overdueRecoed.getBlacklistId() == 0){
                    overdueRecoed.setBlacklistId(blacklist.getBlacklistId());
                    overdueRecoed.setDefaultState(1);
                    overdueAmount += 1;
                }
                //逾期3天
                else if (differDays == 3) {
                    defaultThreeDayAmount += 1;
                }
                //逾期4天
                else if (differDays >= 4) {
                    defaultFourAndMordDayAmount += 1;
                }
                //给该分期记录添加标记字段
                //预留添加违约金位置

                //本预期的总计金额
                totaldefaultMoney = ((overdueRecoed.getReturnMoney().add(overdueRecoed.getReturnDividend())).multiply(new BigDecimal(differDays))).multiply(serviceRate).setScale(8,BigDecimal.ROUND_UP);
                //该比借款新产生的违约金
                newDefaultMoney = totaldefaultMoney.subtract(overdueRecoed.getDefaultMoney());
                //更新分期的违约金状况
                overdueRecoed.setDefaultMoney(totaldefaultMoney);
                //更新预期未还状态
                overdueRecoed.setRepayStatus(1);
                //将修改后的分期记录直接保存
                borrowingInstallmentRepository.save(overdueRecoed);
                //主记录添加逾期状态
                BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(overdueRecoed.getBorrowingRecordId());
                borrowingRecord.setDefaultStatus(1);
                borrowingRecord.setRepayStatus(1);
                borrowingRecordRepository.save(borrowingRecord);
                //循环位数
                int a = 0;
                //添加信用档案
                switch (borrowingRecord.getBorrowingType()) {
                    case 10:
                        btcCreditFile.setOverdueNoRepayAmount(overdueStrokeCount);
                        btcCreditFile.setOverdueNoRepayBalance(btcCreditFile.getOverdueNoRepayBalance().add(overdueRecoed.getDefaultMoney().add(overdueRecoed.getReturnMoney().add(overdueRecoed.getReturnDividend()))));
                        btcCreditFile.setReturnedBalance(btcCreditFile.getReturnedBalance().add(newDefaultMoney));
                        break;
                    case 20:
                        ethCreditFile.setOverdueNoRepayAmount(overdueStrokeCount);
                        ethCreditFile.setOverdueNoRepayBalance(ethCreditFile.getOverdueNoRepayBalance().add(overdueRecoed.getDefaultMoney().add(overdueRecoed.getReturnMoney().add(overdueRecoed.getReturnDividend()))));
                        ethCreditFile.setReturnedBalance(ethCreditFile.getReturnedBalance().add(newDefaultMoney));
                        break;
                    case 100:
                        bhpcCreditFile.setOverdueNoRepayAmount(overdueStrokeCount);
                        bhpcCreditFile.setOverdueNoRepayBalance(bhpcCreditFile.getOverdueNoRepayBalance().add(overdueRecoed.getDefaultMoney().add(overdueRecoed.getReturnMoney().add(overdueRecoed.getReturnDividend()))));
                        bhpcCreditFile.setReturnedBalance(bhpcCreditFile.getReturnedBalance().add(newDefaultMoney));
                        break;
                }
            }
            addBlacklist(blacklists, creditFiles, defaultFourAndMordDayAmount, defaultThreeDayAmount, overdueAmount, blacklist, overdueLevel, nowDayStartDate, releaseDate, btcCreditFile, ethCreditFile, bhpcCreditFile);

            //保存黑名单
            blacklistRepository.save(blacklists);
            //保存信用档案表
            creditFileRepository.save(creditFiles);
        }

    }

    /**
     * @author zwj
     * @date 2018-08-27 09:08
     * @@Description: 移除黑名单用户
     */
    public void releaseBlacklist() {
        logger.info("BlackList  releaseJob is Starting ...");
        Date nowDayStartDate = FormatUtil.getDayStartDay(new Date());
        //释放黑名单
        List<Blacklist> blacklists = blacklistRepository.findByReleaseDateAndIsExist(nowDayStartDate, 1);
        for (Blacklist blacklist : blacklists) {
            if(blacklist.getOverdueLevel() !=3 ){
                logger.info("移除黑名单任务 用户id:" + blacklist.getUserId() + "黑名单id:" + blacklist.getBlacklistId());
                blacklist.setIsExist(2);
            }
        }
        blacklistRepository.save(blacklists);
    }

    /**
     * @author zwj
     * @date 2018-08-26 19:44
     * @@Description: 初始化黑名单
     */
    public Blacklist blicklistInitiallze(Long userId) {
        Blacklist blacklist = new Blacklist();
        blacklist.setUserId(userId);
        blacklist.setOverdueAmount(0);
        blacklist.setOverdueLevel(0);
        blacklist.setIsExist(1);
        return blacklist;
    }

    ;

    /**
     * @author zwj
     * @date 2018-08-26 19:44
     * @@Description: 修改黑名单 -- 修改黑名单 1.加入时间，2.加入原因 3.处罚时间 4.逾期次数 5.逾期等级
     */
    public Blacklist blacklistChange(Blacklist blacklist, Date createDate, Integer createReason, Date releaseDate, Integer overdueAmount, Integer overdueLevel) {
        blacklist.setCreateDate(createDate);
        blacklist.setCreateReason(createReason);
        blacklist.setOverdueAmount(overdueAmount);
        blacklist.setReleaseDate(releaseDate);
        blacklist.setOverdueLevel(overdueLevel);
        return blacklist;
    }

    /**
     * @author zwj
     * @date 2018-08-27 16:18
     * @@Description: 信用档案每日归档
     */
    public CreditFile creditFileInit(Long userId, Integer walletType) {
        CreditFile creditFile = creditFileRepository.findByUserIdAndWalletType(userId, walletType);
        if (!ParamUtil.isBlack(creditFile)) {
            creditFile.setOverdueNoRepayAmount(0);
            creditFile.setOverdueNoRepayBalance(new BigDecimal(BigInteger.ZERO));
        }
        return creditFile;
    }

    /**
     * @author zwj
     * @date 2018-08-27 16:51
     * @@Description: 黑名单判断
     */
    public void addBlacklist(List<Blacklist> blacklists, List<CreditFile> creditFiles, int defaultFourAndMordDayAmount, int defaultThreeDayAmount, int overdueAmount, Blacklist blacklist, Integer overdueLevel, Date nowDayStartDate, Date releaseDate, CreditFile btcCreditFile, CreditFile ethCreditFile, CreditFile bhpcCreditFile) {
        //更新此用户黑名单
        //依据逾期天数判定
        Integer createReason = 1 ;
        Integer count = 0;
        //C级黑名单
        if (defaultFourAndMordDayAmount >= 2 || 20 <= overdueAmount) {
            //如果该用户已经是c级用户
            if (overdueLevel == 3) {
                count = mortgageMoneyDisposeInC(nowDayStartDate,blacklist.getUserId(),blacklist.getBlacklistId(),blacklist,btcCreditFile,ethCreditFile,bhpcCreditFile);
                blacklist.setOverdueAmount(overdueAmount+count);
            }
            //否则创建个新的黑名单c级用户
            else {
                //天数多加90天，变为180天
                releaseDate = FormatUtil.addDay(releaseDate,90);
                //要是逾期天数为2的笔数大等于2 则加入原因变为3
                if(defaultFourAndMordDayAmount >= 2 ){
                    createReason = 3 ;
                }
                //操作borrowingRecord borrowingInstallment 以及叠加多余的逾期次数
                count = mortgageMoneyDispose(defaultFourAndMordDayAmount,overdueAmount,nowDayStartDate,blacklist.getUserId(),blacklist.getBlacklistId(),blacklist,btcCreditFile,ethCreditFile,bhpcCreditFile);
                blacklist = blacklistChange(blacklist, nowDayStartDate, createReason, releaseDate, overdueAmount+count, 3);
            }
        }
        //期数B级黑名单
        else if (defaultThreeDayAmount >= 1 || (10 <= overdueAmount && overdueAmount < 20)) {
            if(defaultThreeDayAmount >= 1 ){
                createReason = 2 ;
            }
            //如果此用户逾期次数到达C级则直接创建C级用户类
            //如果用户已经是b级或者c级黑名单用户 则直接更新其次数
            if (overdueLevel == 2 || overdueLevel == 3) {
                blacklist.setOverdueAmount(overdueAmount);
            }
            //如果比起等级低则创建b级用户
            else {
                blacklist = blacklistChange(blacklist, nowDayStartDate, createReason, releaseDate, overdueAmount, 2);
            }
        }
        //A级黑名单
        else if (3 <= overdueAmount && overdueAmount < 10) {
            //如果还评不上等级的话
            if (overdueLevel == 0) {
                blacklist = blacklistChange(blacklist, nowDayStartDate, createReason, releaseDate, overdueAmount, 1);
            } else {
                blacklist.setOverdueAmount(overdueAmount);
            }
        }
        //依据逾期次数判定 此用户逾期次数小于3
        else if (overdueAmount < 3) {
            blacklist.setOverdueAmount(overdueAmount);
        }
        //将此黑名单信息存入黑名单集合
        blacklists.add(blacklist);
        //存储该用户信用档案
        if (!ParamUtil.isBlack(btcCreditFile)) {
            creditFiles.add(btcCreditFile);
        }
        if (!ParamUtil.isBlack(ethCreditFile)) {
            creditFiles.add(ethCreditFile);
        }
        if (!ParamUtil.isBlack(bhpcCreditFile)) {
            creditFiles.add(bhpcCreditFile);
        }
    }
    /**
     * @author zwj
     * @date 2018-09-12 10:21
     * @@Description:没收抵押资产，新成为C级用户资产没收
     */
    public Integer mortgageMoneyDispose(Integer defaultFourAndMordDayAmount,Integer overdueAmount,Date nowDayStartDate,Long userId,Long blacklistId,Blacklist blacklist,CreditFile btcCreditFile, CreditFile ethCreditFile, CreditFile bhpcCreditFile){
        //处理，可能因为天，可能因为笔数 2个处理
        Date nowDateEndDate = FormatUtil.getEndYesterday(nowDayStartDate);
        Date fourDaysDateBack = FormatUtil.addDay(nowDateEndDate,-3);
        //容器
        List<BorrowingInstallment> borrowingInstallments = new LinkedList<>();
        List<BorrowingRecord> borrowingRecords = new LinkedList<>();
        //再次叠加次数
        Integer count = 0;
        //1.只因为逾期天数过多进入
        if( defaultFourAndMordDayAmount >= 2 && overdueAmount < 20 ){
            List<BorrowingInstallment> allBorrowingRecordId = borrowingInstallmentRepository.getFiltrateOverdueRecord(userId,fourDaysDateBack);
            count = borrowingInstallmentAndBorrowingRecordUpdate(allBorrowingRecordId,borrowingInstallments,borrowingRecords,blacklistId,count,btcCreditFile,ethCreditFile,bhpcCreditFile);
        }
        //2.因为笔数过多进入或者  同时因为笔数过多，天数过多进入那么则没收所有的逾期资产
        else {
            List<BorrowingInstallment> allBorrowingRecordId = borrowingInstallmentRepository.getFiltrateOverdueRecord(userId,nowDateEndDate);
            count = borrowingInstallmentAndBorrowingRecordUpdate(allBorrowingRecordId,borrowingInstallments,borrowingRecords,blacklistId,count,btcCreditFile,ethCreditFile,bhpcCreditFile);
        }
        borrowingInstallmentRepository.save(borrowingInstallments);
        borrowingRecordRepository.save(borrowingRecords);
        return count;
    }
    /**
     * @author zwj
     * @date 2018-09-12 16:20
     * @@Description:没收抵押资产，有逾期都没收
     */
    public Integer mortgageMoneyDisposeInC(Date nowDayStartDate,Long userId,Long blacklistId,Blacklist blacklist,CreditFile btcCreditFile, CreditFile ethCreditFile, CreditFile bhpcCreditFile){
        //处理，可能因为天，可能因为笔数 2个处理
        Date nowDateEndDate = FormatUtil.getEndYesterday(nowDayStartDate);
        //容器
        List<BorrowingInstallment> borrowingInstallments = new LinkedList<>();
        List<BorrowingRecord> borrowingRecords = new LinkedList<>();
        List<BorrowingInstallment> allBorrowingRecordId = borrowingInstallmentRepository.getFiltrateOverdueRecord(userId,nowDateEndDate);
        Integer count = 0;
        count = borrowingInstallmentAndBorrowingRecordUpdate(allBorrowingRecordId,borrowingInstallments,borrowingRecords,blacklistId,count,btcCreditFile,ethCreditFile,bhpcCreditFile);
        borrowingInstallmentRepository.save(borrowingInstallments);
        borrowingRecordRepository.save(borrowingRecords);
        return count;
    }
    /**
     * @author zwj
     * @date 2018-09-12 14:23
     * @@Description:对资产扣押后分期，总记录操作
     */
    public Integer borrowingInstallmentAndBorrowingRecordUpdate(List<BorrowingInstallment> allBorrowingRecordId,List<BorrowingInstallment> borrowingInstallments,List<BorrowingRecord> borrowingRecords,Long blacklistId,Integer count,CreditFile btcCreditFile, CreditFile ethCreditFile, CreditFile bhpcCreditFile){
        for (BorrowingInstallment borrowingInstallment:allBorrowingRecordId){
            //for循环本期需要处理资产的抵押记录
            //取到该记录详情
            BorrowingRecord borrowingRecord = borrowingRecordRepository.findOne(borrowingInstallment.getBorrowingRecordId());
            //钱包处理
            walletBanceAndPledgeFreezeBalanceOperation(borrowingRecord.getMortgageType(), borrowingRecord.getUserId(), borrowingRecord.getMortgageMoney().multiply(new BigDecimal("-1")) ,borrowingRecord.getMortgageMoney().multiply(new BigDecimal("-1")));
            //取出该笔的分期明细
            List<BorrowingInstallment> borrowingInstallmentList = borrowingInstallmentRepository.findByBorrowingRecordId(borrowingRecord.getBorrowingRecordId());

            BigDecimal totalActualRepaymentMoney = new BigDecimal(BigInteger.ZERO);
            BigDecimal overdueNoRepayBalance = new BigDecimal(BigInteger.ZERO);
            for (BorrowingInstallment borrowingInstallment1:borrowingInstallmentList){
                //已经还过的不做处理
                if(borrowingInstallment1.getActualRepaymentDate() == null){
                    borrowingInstallment1.setActualRepaymentDate(new Date());
                    borrowingInstallment1.setActualRepaymentMoney(borrowingInstallment1.getReturnMoney().add(borrowingInstallment1.getReturnDividend().add(borrowingInstallment1.getDefaultMoney())));
                    borrowingInstallment1.setRepayStatus(2);
                    if(borrowingInstallment1.getBlacklistId() == 0){
                        count += 1;
                    }
                    else{
                        overdueNoRepayBalance = overdueNoRepayBalance.add(borrowingInstallment1.getReturnDividend().add(borrowingInstallment1.getDefaultMoney().add(borrowingInstallment1.getReturnMoney())));
                        System.out.println("逾期未还的钱"+overdueNoRepayBalance);
                    }
                    borrowingInstallment1.setBlacklistId(blacklistId);
                    borrowingInstallment1.setDefaultState(1);
                    totalActualRepaymentMoney = totalActualRepaymentMoney.add(borrowingInstallment1.getReturnMoney().add(borrowingInstallment1.getReturnDividend().add(borrowingInstallment1.getDefaultMoney())));
                    borrowingInstallments.add(borrowingInstallment1);
                }
            }
            System.out.println("totalActualRepaymentMoney:"+totalActualRepaymentMoney);
            //添加信用档案
            switch (borrowingRecord.getBorrowingType()) {
                case 10:
                    btcCreditFile.setOverduePayOffAmount(btcCreditFile.getOverduePayOffAmount()+1);
                    btcCreditFile.setOverduePayOffBalance(btcCreditFile.getOverduePayOffBalance().add(totalActualRepaymentMoney));
                    btcCreditFile.setOverdueNoRepayAmount(btcCreditFile.getOverdueNoRepayAmount()-1);
                    btcCreditFile.setOverdueNoRepayBalance(btcCreditFile.getOverdueNoRepayBalance().subtract(overdueNoRepayBalance));
                    btcCreditFile.setRepaidBalance(btcCreditFile.getRepaidBalance().add(totalActualRepaymentMoney));
                    btcCreditFile.setReturnedBalance(btcCreditFile.getReturnedBalance().subtract(totalActualRepaymentMoney));
                    break;
                case 20:
                    ethCreditFile.setOverduePayOffAmount(ethCreditFile.getOverduePayOffAmount()+1);
                    ethCreditFile.setOverduePayOffBalance(ethCreditFile.getOverduePayOffBalance().add(totalActualRepaymentMoney));
                    ethCreditFile.setOverdueNoRepayAmount(ethCreditFile.getOverdueNoRepayAmount()-1);
                    ethCreditFile.setOverdueNoRepayBalance(ethCreditFile.getOverdueNoRepayBalance().subtract(overdueNoRepayBalance));
                    ethCreditFile.setRepaidBalance(ethCreditFile.getRepaidBalance().add(totalActualRepaymentMoney));
                    ethCreditFile.setReturnedBalance(ethCreditFile.getReturnedBalance().subtract(totalActualRepaymentMoney));
                    break;
                case 100:
                    System.out.println(bhpcCreditFile);
                    bhpcCreditFile.setOverduePayOffAmount(bhpcCreditFile.getOverduePayOffAmount()+1);
                    bhpcCreditFile.setOverduePayOffBalance(bhpcCreditFile.getOverduePayOffBalance().add(totalActualRepaymentMoney));
                    bhpcCreditFile.setOverdueNoRepayAmount(bhpcCreditFile.getOverdueNoRepayAmount()-1);
                    bhpcCreditFile.setOverdueNoRepayBalance(bhpcCreditFile.getOverdueNoRepayBalance().subtract(overdueNoRepayBalance));
                    bhpcCreditFile.setRepaidBalance(bhpcCreditFile.getRepaidBalance().add(totalActualRepaymentMoney));
                    bhpcCreditFile.setReturnedBalance(bhpcCreditFile.getReturnedBalance().subtract(totalActualRepaymentMoney));
                    System.out.println(bhpcCreditFile);
                    break;
            }
            //状态
            borrowingRecord.setRepayStatus(2);
            //归还进度
            if(borrowingRecord.getRefundWay() ==1){
                borrowingRecord.setProgress(1);
            }
            else{
                borrowingRecord.setProgress(borrowingRecord.getDeadline());
            }
            borrowingRecord.setStatus(3);
            borrowingRecord.setDefaultStatus(1);
            //添加
            borrowingRecords.add(borrowingRecord);
        }
        return count;
    }
    /**
     * @author zwj
     * @date 2018-09-04 14:34
     * @@Description:钱包可用资产，冻结资产的改变
     */
    public void walletBanceAndPledgeFreezeBalanceOperation(Integer walletType, Long userId, BigDecimal balance ,BigDecimal pledgeFreezeBalance){
        switch (walletType) {
            case 10:
                BitCoinWallet bitCoinWallet = bitcoinWalletRepository.findByUserId(userId);
                bitCoinWallet.setBalance(bitCoinWallet.getBalance().add(balance));
                bitCoinWallet.setPledgeFreezeBalance(bitCoinWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                bitcoinWalletRepository.save(bitCoinWallet);
                break;
            case 20:
                EthereumWallet ethereumWallet = ethereumWalletRepository.findByUserId(userId);
                ethereumWallet.setBalance(ethereumWallet.getBalance().add(balance));
                ethereumWallet.setPledgeFreezeBalance(ethereumWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                ethereumWalletRepository.save(ethereumWallet);
                break;
            case 100:
                BhpcWallet bhpcWallet = bhpcWalletRepository.findByUserId(userId);
                bhpcWallet.setBalance(bhpcWallet.getBalance().add(balance));
                bhpcWallet.setPledgeFreezeBalance(bhpcWallet.getPledgeFreezeBalance().add(pledgeFreezeBalance));
                bhpcWalletRepository.save(bhpcWallet);
                break;
        }
    }
}
